/**
 * @author Kristian Mandrup
 * @date June 10, 2009
 * @version 0.1
 * 
 * m3gen :: NodeObjBuilder
 * 
 * Builds a nodeObj from resolved content
 * 
 * Usage 
 * -----
 * var NodeObjBuilder = this.getNodeObjBuilder()
 * var myNodeObjBuilder = new NodeObjBuilder(createOptions);
 * var nodeObj = myNodeObjBuilder.execute(resolvedContent);
 */

/**
 * NodeObjBuilder constructor
 * Sets up NodeObjBuilder for use
 * 
 */
m3gen.NodeObjBuilder = function(createOptions) {
	if (arguments.length < 1 || m3gen.util.invalidOptions(createOptions))
		return;
	this.init(createOptions);		
}

// inherit from Parser
// m3gen.NodeObjBuilder.inherits(m3gen.Parser);
m3gen.NodeObjBuilder.inherits(m3gen.Base);

m3gen.NodeObjBuilder.prototype.init = function(createOptions){	
	this.setClassName('NodeObjBuilder');		

	// call Parser superclass initializer 
	this.uber('init', createOptions);
}

/**
 * Public
 * Builds a nodeObj from resolved content.
 * 
 * FLOW:
 * - 
 * 
 * @param 	{Object} resolvedContent - contains content resolved from a jsonObj
 * @return 	{Object} nodeObj
 * 
 * 
	var resolvedContent = {
		branch: true/false
		id: id,
		unique_id: unique_id
		state: state,
		icon: icon,
		title: title,
		languageData: languageData			
	}
 */

/**
 * doExecute default empty wrapper
 * @param {Object} options
 * 
 * @return {NodeObject}	nodeObj	a simple nodeObj without children
 * 
 * nodeObj = {
 * 	attributes 
 *  state (for branch only)
 *  languageData (if languageOn only)
 *  data (if language not on) 
 */
m3gen.NodeObjBuilder.prototype.execute = function(options){
	return this.doExecute(options);
}


m3gen.NodeObjBuilder.prototype.doExecute = function (resolvedContent) {	
	// init nodeObj, always has an Id
	var that = this; //.getTheContext(this); 
	var idObj = that.decideNodeObjId(resolvedContent);	
	var nodeObj = {
		id: idObj,
		isBranch: false
	};	
	that.setAttributes(nodeObj, resolvedContent);
	that.setState(nodeObj, resolvedContent);
	/*
	that.setRelation(nodeObj, resolvedContent);
	that.setClass(nodeObj, resolvedContent);
	*/
	that.setData(nodeObj, resolvedContent);
	if (this.getLanguageModeOn()) {
		that.setLanguageData(nodeObj, resolvedContent);
	}	
	return nodeObj;
}

/**
 * Set attributes of nodeObj
 * 
 * @param {Object} nodeObj
 * @param {Object} resolvedContent
 * 
 * @return {NodeObject} nodeObj
 */
m3gen.NodeObjBuilder.prototype.setAttributes = function(nodeObj, resolvedContent) {
	var attributes = this.getResolvedAttributes(resolvedContent);
	// attributes
	if (attributes) {
		nodeObj.attributes = attributes;
	}
	return nodeObj;
}

/**
 * Set state of nodeObj (if branch)
 * 
 * @param {Object} nodeObj
 * @param {Object} resolvedContent
 * 
 * @return {NodeObject} nodeObj
 */
m3gen.NodeObjBuilder.prototype.setState = function(nodeObj, resolvedContent) {
	var branch = this.isResolvedBranch(resolvedContent);		
	var state = this.getResolvedState(resolvedContent);	
	var attributes = this.getResolvedAttributes(resolvedContent);
	// state
	if (state && branch)
		nodeObj.state = state;	
	return nodeObj;
}

/**
 * Set languageData of nodeObj
 * 
 * @param {Object} nodeObj
 * @param {Object} resolvedContent
 * 
 * @return {NodeObject} nodeObj
 */
m3gen.NodeObjBuilder.prototype.setLanguageData = function(nodeObj, resolvedContent) {
	var languageData = this.getResolvedLanguageData(resolvedContent);
	// languageData
	if (languageData)
		nodeObj.languageData = languageData;	
	return nodeObj;
}

/**
 * Set Data of nodeObj
 * 
 * @param {Object} nodeObj
 * @param {Object} resolvedContent
 * 
 * @return {NodeObject} nodeObj
 */
m3gen.NodeObjBuilder.prototype.setData = function(nodeObj, resolvedContent) {
	var data = this.getResolvedData(resolvedContent);		
	// data		
	if (data)
		nodeObj.data = data;	
	return nodeObj;
}



